function ProductView() {
	this.parent();
}

ProductView.inherits(BaseView, {
	init : function() {
		this.createLoadProductNode(this.ctrl.model.books);
	},
	createLoadProductNode : function(arrBooks) {
		var key = "product";
		addActive(key);
		var rootNode = $id("loadBook");
		rootNode.innerHTML = "";
		if (arrBooks.length > 0) {
			//create new table
			var nodeTable = createTable();
			nodeTable.setAttribute("id", "tableProduct");
			var nodeThId = createNode("th");
			var IdTh = createTextNode("Mã sách");
			nodeThId.appendChild(IdTh);
			var nodeThBName = createNode("th");
			var bNameTh = createTextNode("Tên sách");
			nodeThBName.appendChild(bNameTh);
			var nodeThCate = createNode("th");
			var cateTh = createTextNode("Thể loại");
			nodeThCate.appendChild(cateTh);
			var nodeThAu = createNode("th");
			var auTh = createTextNode("Tác giả");
			nodeThAu.appendChild(auTh);
			var nodeThPrice = createNode("th");
			var priceTh = createTextNode("Giá");
			nodeThPrice.appendChild(priceTh);
			var nodeThDes = createNode("th");
			var desTh = createTextNode("Mô tả");
			nodeThDes.appendChild(desTh);
			var nodeThQuan = createNode("th");
			var quanTh = createTextNode("Số lượng");
			nodeThQuan.appendChild(quanTh);
			var nodeThImg = createNode("th");
			var imgTh = createTextNode("Hình ảnh");
			nodeThImg.appendChild(imgTh);
			var nodeThCount = createNode("th");
			var countTh = createTextNode("Lượt xem");
			nodeThCount.appendChild(countTh);
			var nodeThUpdate = createNode("th");
			var nodeThDelete = createNode("th");

			nodeTable.appendChild(nodeThId);
			nodeTable.appendChild(nodeThBName);
			nodeTable.appendChild(nodeThCate);
			nodeTable.appendChild(nodeThAu);
			nodeTable.appendChild(nodeThPrice);
			nodeTable.appendChild(nodeThDes);
			nodeTable.appendChild(nodeThQuan);
			nodeTable.appendChild(nodeThImg);
			nodeTable.appendChild(nodeThCount);
			nodeTable.appendChild(nodeThUpdate);
			nodeTable.appendChild(nodeThDelete);

			var nodeTr = null;
			var book = null;
			var nodeId = null;
			var nodeBName = null;
			var nodeCate = null;
			var nodeAu = null;
			var nodePrice = null;
			var nodeDes = null;
			var nodeQuan = null;
			var nodeCount = null;
			var nodeTdImg = null;
			var nodeImg = null;
			var nodeUpdate = null;
			var nodeTdUpdate = null;
			var nodeDivUpdate = null;
			var nodeDelete = null;
			var nodeTdDelete = null;
			var nodeDivDelete = null;
			var length = arrBooks.length;

			for (var i = 0; i < length; i++) {
				book = arrBooks[i];
				nodeTr = createNode("tr");
				nodeTr.setAttribute("id", "book" + book.bookId);

				//create node bookid
				nodeId = createNode("td");
				var bookid = createTextNode(book.bookId);
				nodeId.appendChild(bookid);

				//create node book name
				nodeBName = createNode("td");
				var bookName = createTextNode(book.bookName);
				nodeBName.appendChild(bookName);

				//create node category
				nodeCate = createNode("td");
				var category = createTextNode(book.categoryId);
				nodeCate.appendChild(category);

				//create node author
				nodeAu = createNode("td");
				var author = createTextNode(book.author);
				nodeAu.appendChild(author);

				//create node price
				nodePrice = createNode("td");
				var price = createTextNode(book.price);
				nodePrice.appendChild(price);

				//create node description
				nodeDes = createNode("td");
				nodeDivDes = createDiv();
				var des = createTextNode(book.des);
				nodeDivDes.addClass("heightTd");
				nodeDivDes.appendChild(des);
				nodeDes.appendChild(nodeDivDes);
				//create node description
				nodeQuan = createNode("td");
				var quan = createTextNode(book.quan);
				nodeQuan.appendChild(quan);

				//create node image
				nodeTdImg = createNode("td");
				nodeImg = createNode("img");
				nodeImg.setAttribute("src",book.imgUrl);
				nodeImg.setAttribute("width",150);
				nodeImg.setAttribute("height",200);
				nodeTdImg.appendChild(nodeImg);

				//create node count
				nodeCount = createNode("td");
				var count = createTextNode(book.count);
				nodeCount.appendChild(count);

				//create node update
				nodeTdUpdate = createNode("td");
				nodeDivUpdate = createDiv();
				nodeDivUpdate.addClass("updateProduct");
				nodeUpdate = createNode("img");
				nodeUpdate.setAttribute("src", Pages.params.img + "update.png");
				nodeUpdate.style.cursor = "pointer";
				nodeDivUpdate.appendChild(nodeUpdate);
				nodeTdUpdate.appendChild(nodeDivUpdate);
				//add onClick event listener for class updateProduct
				nodeTdUpdate.addEventListener("click", function(event) {
					var currentRow = $id(event.currentTarget.parentNode.id);
					var cels = currentRow.getElementsByTagName("td");
					var id = cels[0].textContent;
					var bookName = cels[1].textContent;
					var category = cels[2].textContent;
					var author = cels[3].textContent;
					var price = cels[4].textContent;
					var des = cels[5].textContent;
					var quan = cels[6].textContent;
					var imgUrl = cels[7].innerHTML;

					var model = new BookModel(id, bookName, category, author, price, des, quan, imgUrl);

					pagesManager.setPage(Pages.updateProduct, false, model);
				});

				//create node delete
				nodeTdDelete = createNode("td");
				nodeDivDelete = createDiv();
				nodeDelete = createNode("img");
				nodeDelete.setAttribute("src", Pages.params.img + "delete_icon.png");
				nodeDelete.style.cursor = "pointer";
				nodeTdDelete.addEventListener("click", function() {
					var currentRow = $id(event.currentTarget.parentNode.id);
					var cels = currentRow.getElementsByTagName("td");
					var id = cels[0].textContent;
					var r = confirm("Bạn có muốn xóa sách có mã là " + id + " ?");
					var x;
					if (r == true) {
						deleteProduct(id);
					}
				});
				nodeDivDelete.appendChild(nodeDelete);
				nodeTdDelete.appendChild(nodeDivDelete);

				nodeTr.appendChild(nodeId);
				nodeTr.appendChild(nodeBName);
				nodeTr.appendChild(nodeCate);
				nodeTr.appendChild(nodeAu);
				nodeTr.appendChild(nodePrice);
				nodeTr.appendChild(nodeDes);
				nodeTr.appendChild(nodeQuan);
				nodeTr.appendChild(nodeTdImg);
				nodeTr.appendChild(nodeCount);
				nodeTr.appendChild(nodeTdUpdate);
				nodeTr.appendChild(nodeTdDelete);

				nodeTable.appendChild(nodeTr);

			}
			rootNode.appendChild(nodeTable);
			var divPaging = createDiv();
			divPaging.setAttribute("id", "pageNavPosition");
			divPaging.setAttribute("class", "paging");
			var script = createNode("script");
			script.setAttribute("type", "text/javascript");
			script.innerHTML = "var pager = new Pager('tableProduct', 5); pager.init(); pager.showPageNav('pager', 'pageNavPosition'); pager.showPage(1);";
			rootNode.appendChild(divPaging);
			rootNode.appendChild(script);
		}
	},

});

var BookModel = function(id, bname, cate, author, price, des, quan, urlImg) {
	this.id = id;
	this.bname = bname;
	this.cate = cate;
	this.author = author;
	this.price = price;
	this.des = des;
	this.quan = quan;
	this.urlImg = urlImg;

}
